<template>
  <div class="container" style="padding: 30px 30px 100px; position: relative">
    <div
      class="inner"
      style="display: flex; justify-content: center; margin-top: 35px"
    >
      <div class="item">
        <el-card>
          <div class="top">
            <div class="left">
              <p class="title">{{ propertie.title }}</p>
              <img :src="propertie.image" alt="" />
            </div>
            <div class="right">
              <div class="pic">
                <img v-for="item in photos" :src="item" alt="图片加载失败" />
                <!-- <img :src="propertie.photos[1]" alt="图片加载失败" /> -->
                <!-- <img :src="propertie.photos[2]" alt="图片加载失败" /> -->
                <!-- <img :src="propertie.photos[3]" alt="图片加载失败" /> -->
              </div>
              <div class="address">
                <p class="price">{{ propertie.price }}元</p>
                <div id="map"></div>
              </div>
            </div>
          </div>
          <div class="bottom">{{ propertie.desc }}</div>
        </el-card>
      </div>
      <el-button class="back" @click="back" type="text"><<<返回</el-button>
    </div>
  </div>
</template>

<script>
import { getPropertieDetail, getStaticMap } from "@/api/propertie";
import AMapLoader from "@amap/amap-jsapi-loader";
import { getMapCode } from "@/api/map";

// 设置安全密钥
window._AMapSecurityConfig = {
  securityJsCode: "7825945400a147399c81697867df154a",
};

export default {
  data() {
    return {
      location: "",
      map: null,
      propertie: {
        //   id:'',
        //   title:'',
        //   images:'',
        //   photos:'',
        //   price:'',
        //   desc:'',
        //   address:'',
      },
      photos: [],
    };
  },
  created() {
    this.getPropertieDetail();
  },
  mounted() {
    //DOM初始化完成进行地图初始化
    this.initMap();
  },
  methods: {
    async getPropertieDetail() {
      let id = this.$route.params.id;
      console.log(33, id);
      let res = await getPropertieDetail(id);
      console.log(35, res);
      this.propertie = res.data;
      // console.log(45, this.propertie);
      this.photos = res.data.photos;
      // console.log(123456, this.photos);

      let addre = this.propertie.address;
      addre = addre.replace(/\//g, "").replace(/\d+./g, "");
      //   console.log(67, addre);
      let var1 = `address=${addre}&output=JSON&key=992820787b01c01261558c5118c2eb10`;
      let code = await getMapCode(var1);
      // console.log(71,code)
      this.location = code.data.geocodes[0].location;
      console.log(73, this.location);
    },
    back(){
      let id = this.$route.params.id
      this.$router.push("/categories");
    },
    initMap() {
      AMapLoader.load({
        key: "40b903c04e4b8a24eb7d2bc5ed57b754", // 申请好的Web端开发者Key，首次调用 load 时必填
        version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
        plugins: [""], // 需要使用的的插件列表，如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          this.map = new AMap.Map("map", {
            //设置地图容器id
            viewMode: "3D", //是否为3D地图模式
            zoom: 15, //初始化地图级别
            // center: this.location, //初始化地图中心点位置
            center: [113.3612, 23.12468], //初始化地图中心点位置
            resizeEnable: true, // 是否监控地图容器尺寸变化
            showMarker: true, // 定位成功后在定位到的位置显示点标记
          });
        })
        .catch((e) => {
          console.log(e);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.item {
  width: 800px;
  display: flex;
}
.top {
  display: flex;
  height: 560px;
  padding-bottom: 10px;
  margin-bottom: 20px;
  border-bottom: 2px solid rgba(135, 135, 135, 0.363);
  .left {
    width: 640px;
    // height: 300px;
    // background-color: #aaf;
    margin-right: 20px;
    .title {
      width: 200px;
      font-size: 20px;
      text-align: left;
      margin-bottom: 10px;
    }
    img {
      width: 500px;
      height: 500px;
    }
  }
  .right {
    width: 320px;
    // height: 300px;
    margin-top: 36px;
    .pic {
      img {
        width: 100px;
        height: 100px;
        margin-right: 8px;
        margin-bottom: 5px;
      }
    }
    .address {
      .price {
        font-size: 20px;
        font-weight: 600;
        color: rgb(247, 168, 43);
        margin-bottom: 10px;
      }
    }
  }
}
#map {
  overflow: hidden;
  padding: 0px;
  margin: 0px;
  width: 100%;
  height: 250px;
}
.amap-copyright {
  display: none !important;
}
.amap-logo {
  display: none !important;
}
.back{
  height: 40px;
  font-size: 16px;
  margin-left: 40px;
}
</style>